package com.eascs.web.o2o.web.reward.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.web.o2o.web.reward.dao.BaseDao;
import com.eascs.web.o2o.web.reward.entity.ProvinceRewardQueryForm;
import com.eascs.web.o2o.web.reward.entity.ProvinceRewardVo;
import com.eascs.web.o2o.web.reward.service.ProvinceRewardService;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;

@Service
public class ProvinceRewardServiceImpl implements ProvinceRewardService {
	
	private static Logger log = LoggerFactory.getLogger(ProvinceRewardServiceImpl.class);

	@Inject
	BaseDao baseDao;

	@Override
	public PageResult<Object[]> getProvinceRewardDetail(ProvinceRewardQueryForm entity) {
		String sql = "select t.t_month ,t.province_name ,t.rm_qty , t.cm_avg_amount, t.province_overdue_ratio, t.overdue_coefficient ,t.month_bonus from dm_reward_province_month_bonus t where /**DATA_PERMISSION(t.ltd)**/ 1=1 ";
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, entity.getPageSize(), entity.getShowPage());
		StringBuffer querySql = new StringBuffer();
		querySql.append(sql); 
		querySql.append(" AND t.t_month = ?");
		
		sqlPageQuery.addParam(entity.getMonth());
		
		if(!StringUtil.isEmpty(entity.getProvince())){
			querySql.append(" AND t.province_name = ?");
			sqlPageQuery.addParam(entity.getProvince());
		}
	
		sqlPageQuery.setQuerySql(querySql.toString());
		return baseDao.findDataWithPage(sqlPageQuery);
	}

	
	
	@Override
	public PageResult<Object[]> downProvinceRewardDetail(ProvinceRewardQueryForm entity) {
		String sql = "select t.t_month ,t.province_code ,t.province_name ,t.rm_qty , t.cm_avg_amount, t.province_overdue_ratio, t.overdue_coefficient ,t.month_bonus , t.ltd , t.ltd_code , t.ltd_name from dm_reward_province_month_bonus t where 1=1 ";
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, entity.getPageSize(), entity.getShowPage());
		StringBuffer querySql = new StringBuffer();
		querySql.append(sql); 
		querySql.append(" AND t.t_month = ?");
		sqlPageQuery.addParam(entity.getMonth());
		sqlPageQuery.setQuerySql(querySql.toString());
		return baseDao.findDataWithPage(sqlPageQuery);
	}
	
	
	
	@Override
	@Transactional("transactionManagerDbForReward")
	public boolean addProvinceRewardList(List<List<Object>> result){
			User user=UserHolder.getUser();
			//删除当前月份前一个月的所有数据
			baseDao.executeSql(" delete from dm_reward_province_month_bonus where  t_month = DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 month),'%Y-%m')");
			
			//将excel中的数据导入到数据库中
			if(result!=null&&result.size()>0){
				for (int i = 0; i < result.size(); i++) {
					@SuppressWarnings({ "unchecked", "rawtypes" })
					List<Object> list = (List) result.get(i);
					for(int j=0;j<list.size();j++){
						if(list.get(j) == null)
							list.set(j, "0");
					}
					ProvinceRewardVo vo = new ProvinceRewardVo();
					vo.settMonth(list.get(0).toString());
					vo.setProvinceCode(list.get(1).toString());
					vo.setProvinceName(list.get(2).toString());	
					vo.setRmqty(Integer.valueOf(list.get(3).toString()));				
					vo.setCaa(BigDecimal.valueOf(Double.valueOf(list.get(4).toString())));
					vo.setPor(BigDecimal.valueOf(Double.valueOf(list.get(5).toString())));
					vo.setOc(BigDecimal.valueOf(Double.valueOf(list.get(6).toString())));	
					vo.setMb(BigDecimal.valueOf(Double.valueOf(list.get(7).toString())));
					vo.setLtd(list.get(8).toString());
					vo.setLtdCode(list.get(9).toString());
					vo.setLtdName(list.get(10).toString());					
					vo.setIsLock("N");
					vo.setCreate_user(user.getCname());
					vo.setCreate_date(new Date());
					vo.setUpdate_user(user.getCname());
					vo.setUpdate_date(new Date());
					vo.setConfirm_user(user.getCname());
					vo.setConfirm_date(new Date());
					baseDao.saveEntity(vo);
			    	}				
			}else{
				log.error("导入的数据为空，事务回滚");
				throw new RuntimeException("导入的数据为空，请重新导入！");
			}
		   return true;
	}


}
